package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.dto.CommonMaterialsDTO;
import com.eric.domain.po.CommonMaterials;
import com.eric.domain.vo.CommonMaterialsVO;
import com.eric.mapper.CommonMaterialsMapper;
import com.eric.service.CommonMaterialsService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.eric.domain.po.table.CommonMaterialsTableDef.COMMON_MATERIALS;

/**
 *  服务层实现。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@Service
public class CommonMaterialsServiceImpl extends ServiceImpl<CommonMaterialsMapper, CommonMaterials>  implements CommonMaterialsService{

    @Override
    public SaResult saveOneService(CommonMaterialsVO commonMaterialsVO) {
        CommonMaterials one = queryChain().select()
                .where(COMMON_MATERIALS.MATERIAL_NAME.eq(commonMaterialsVO.getMaterialName()))
                .one();
        if (BeanUtil.isEmpty(one)){
            CommonMaterials commonMaterials=new CommonMaterials();
            BeanUtil.copyProperties(commonMaterialsVO,commonMaterials);
            boolean save = save(commonMaterials);
            return save ? SaResult.ok() : (SaResult.error("录入错误"));
        }else {
            return SaResult.error("项目已经存在！");
        }
    }

    @Override
    public SaResult updateByIdService(CommonMaterialsVO commonMaterialsVO) {
        boolean nameEffective=false;
        boolean descriptionEffective =false;
        boolean unitPriceEffective =false;
        boolean unitNameEffective =false;
        boolean materialLSpecificationsEffective =false;

        if (!StrUtil.isBlank(commonMaterialsVO.getMaterialName())){
            nameEffective=true;
        }
        if (!StrUtil.isBlank(commonMaterialsVO.getDescription())){
            descriptionEffective=true;
        }
        if (commonMaterialsVO.getUnitPrice()>=0){
            unitPriceEffective=true;
        }
        if (!StrUtil.isBlank(commonMaterialsVO.getUnitName())){
            unitNameEffective=true;
        }
        if (!StrUtil.isBlank(commonMaterialsVO.getMaterialSpecifications())){
            materialLSpecificationsEffective=true;
        }


        CommonMaterials commonMaterials = queryChain().where(COMMON_MATERIALS.MATERIAL_NAME.eq(StrUtil.trim(commonMaterialsVO.getMaterialName()))).one();

        if (!BeanUtil.isEmpty(commonMaterials)&& !Objects.equals(commonMaterials.getId(),commonMaterialsVO.getId())){
            return SaResult.error("物料名称已经存在！");
        }

        boolean updated = UpdateChain.of(CommonMaterials.class)
                .set(COMMON_MATERIALS.MATERIAL_NAME, commonMaterialsVO.getMaterialName(), nameEffective)
                .set(COMMON_MATERIALS.DESCRIPTION, commonMaterialsVO.getDescription(), descriptionEffective)
                .set(COMMON_MATERIALS.UNIT_PRICE, commonMaterialsVO.getUnitPrice(), unitPriceEffective)
                .set(COMMON_MATERIALS.UNIT_NAME, commonMaterialsVO.getUnitName(), unitNameEffective)
                .set(COMMON_MATERIALS.MATERIAL_SPECIFICATIONS, commonMaterialsVO.getMaterialSpecifications(), materialLSpecificationsEffective)
                .where(COMMON_MATERIALS.ID.eq(commonMaterialsVO.getId()))
                .update();
        return SaResult.data(updated);
    }

    @Override
    public SaResult getListByPageService(Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }
        List<CommonMaterials> commonMaterialsList = queryChain().select()
                .from(COMMON_MATERIALS).orderBy(COMMON_MATERIALS.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        if (!commonMaterialsList.isEmpty()){
            List<CommonMaterialsDTO> commonMaterialsDTOList = BeanUtil.copyToList(commonMaterialsList, CommonMaterialsDTO.class);
            return SaResult.data(commonMaterialsDTOList);
        }

        return SaResult.error("没有数据");
    }

    @Override
    public SaResult getCommonMaterialsListByNameService(String name, Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }

        List<CommonMaterials> commonMaterialsList = queryChain().select()
                .where(COMMON_MATERIALS.MATERIAL_NAME.like(name, true))
                .orderBy(COMMON_MATERIALS.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();

        if (!commonMaterialsList.isEmpty()){
            List<CommonMaterialsDTO> commonMaterialsDTOList = BeanUtil.copyToList(commonMaterialsList, CommonMaterialsDTO.class);
            return SaResult.data(commonMaterialsDTOList);
        }
        return SaResult.error("没有数据！");
    }

    @Override
    public SaResult getCommonMaterialsListByNameCountService(String name) {
        List<CommonMaterials> commonMaterialsList = queryChain().select().where(COMMON_MATERIALS.MATERIAL_NAME.like(name, true)).list();
        int size = commonMaterialsList.size();
        return SaResult.data(size);
    }
}
